package com.taoym.voucher.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.taoym.voucher.admin.domain.SysActivity;
import com.taoym.voucher.admin.domain.XcxActivity;
import com.taoym.voucher.admin.domain.XcxPromotion;
import com.taoym.voucher.admin.entity.dto.XcxAddActivityDTO;
import com.taoym.voucher.admin.entity.vo.XcxActivityVO;
import com.taoym.voucher.admin.mapper.XcxActivityMapper;
import com.taoym.voucher.admin.mapper.XcxPromotionMapper;
import com.taoym.voucher.admin.service.SysActivityService;
import com.taoym.voucher.admin.service.XcxActivityService;
import com.taoym.voucher.common.enums.ActivityPlatformEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @coder taoym
 * @datetime 2021/7/1 16:13
 * @desc 活动信息服务接口
 */
@Service
@Slf4j
public class XcxActivityServiceImpl implements XcxActivityService {

    @Resource
    private XcxActivityMapper xcxActivityMapper;
    @Resource
    private XcxPromotionMapper xcxPromotionMapper;
    @Resource
    private SysActivityService sysActivityService;

    @Override
    public List<XcxActivityVO> listActivities() {
        List<XcxActivity> xcxActivities = xcxActivityMapper.selectList(new QueryWrapper<>());
        if (CollectionUtils.isEmpty(xcxActivities)) {
            return null;
        }
        List<XcxActivityVO> xcxActivityVOS = new ArrayList<>(xcxActivities.size());
        xcxActivities.forEach(e -> {
            XcxActivityVO xcxActivityVO = new XcxActivityVO();
            xcxActivityVO.setIcon(e.getIcon());
            xcxActivityVO.setActName(e.getActName());
            xcxActivityVO.setImage(e.getImage());
            xcxActivityVO.setPlatform(ActivityPlatformEnum.getMsg(e.getPlatform()));
            xcxActivityVO.setCreateTime(e.getCreateTime());
            xcxActivityVO.setActId(e.getActId());
            xcxActivityVO.setTitle(e.getTitle());
            xcxActivityVOS.add(xcxActivityVO);
        });

        return xcxActivityVOS;
    }

    @Transactional
    @Override
    public Boolean deleteActivity(Integer actId) {

        xcxActivityMapper.delete(new QueryWrapper<XcxActivity>().eq("act_id", actId));
        xcxPromotionMapper.delete(new QueryWrapper<XcxPromotion>().eq("act_id", actId));

        return true;
    }

    @Override
    public Integer addActivity(XcxAddActivityDTO data) {

        SysActivity sysActivity = sysActivityService.actInfo(data.getActId());

        XcxActivity xcxActivity = new XcxActivity();
        xcxActivity.setTitle(data.getTitle());
        xcxActivity.setActId(data.getActId());
        xcxActivity.setIcon(data.getIcon());
        xcxActivity.setMainAct(data.getMainAct());
        xcxActivity.setActName(data.getActName());
        xcxActivity.setPlatform(data.getPlatform());
        xcxActivity.setCreateTime(LocalDateTime.now());
        xcxActivity.setImage(sysActivity.getImg());

        return xcxActivityMapper.insert(xcxActivity);
    }
}
